package belajarzk.ui.util;

import java.util.Locale;

import org.hibernate.Session;
import org.zkoss.web.Attributes;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Hbox;

import belajarzk.common.Common;
import belajarzk.database.hibernate.HibernateUtil;
import belajarzk.database.model.Tbmuser;
import belajarzk.ui.util.MyTolbarbutton;


public class LaguagePanel extends Hbox {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1002566922784685337L;

	public LaguagePanel() {
		super();
		init();
	}

	public LaguagePanel(Component[] children) {
		super(children);
		init();
	}

	private void init() {
		setWidth("50px");
		setHeight("20px");
		// Toolbar toolbar = new Toolbar();
		// toolbar.setParent(this);
		MyTolbarbutton cancel = new MyTolbarbutton("", "/img/id.png");
		cancel.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event arg0) throws Exception {
				Sessions.getCurrent().setAttribute(Attributes.PREFERRED_LOCALE,
						Common.locale);
				Locale locale = (Locale) Sessions.getCurrent().getAttribute(
						Attributes.PREFERRED_LOCALE);

				Tbmuser tbmuser = Common.getCurrentUser();

				tbmuser.setBahasa(Common.locale.getCountry());
				Session session = HibernateUtil.currentSession();
				
				session.update(session.merge(tbmuser));
				

				System.out.println("locale = " + locale.getCountry());
				Executions.getCurrent().sendRedirect("main.zul");
			}
		});
		cancel.setParent(this);
		MyTolbarbutton save = new MyTolbarbutton("", "/img/england.png");
		save.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event arg0) throws Exception {
				Sessions.getCurrent().setAttribute(Attributes.PREFERRED_LOCALE,
						Common.localeEn);
				Locale locale = (Locale) Sessions.getCurrent().getAttribute(
						Attributes.PREFERRED_LOCALE);
				Tbmuser tbmuser = Common.getCurrentUser();

				tbmuser.setBahasa(Common.localeEn.getCountry());
				Session session = HibernateUtil.currentSession();
				
				session.update(session.merge(tbmuser));
				
				System.out.println("locale = " + locale.getCountry());
				Executions.getCurrent().sendRedirect("main.zul");
			}
		});
		save.setParent(this);
	}
}
